package com.situ.student.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.entity.Student;
import com.situ.student.service.IStudentService;
import com.situ.student.service.impl.StudentServiceImpl;

/**
 * 和学生相关的Controller
 */
public class StudentController extends HttpServlet {
	private IStudentService studentService = new StudentServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// post请求乱码问题解决
		req.setCharacterEncoding("utf-8");
		List<Student> list = studentService.list();
		for (Student student : list) {
			System.out.println("student="+student);
		}
		// 将list放到req里面
		req.setAttribute("list", list);
		// 转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_html_list.jsp").forward(req, resp);
	}

	public List<Student> list() {
		return studentService.list();
	}

	public boolean insert(String name, int age, String gender, Integer banjiId) {
		Student student = new Student(name, age, gender, banjiId);
		return studentService.insert(student);
	}

	public boolean deleteById(Integer id) {
		return studentService.deleteById(id);
	}

}
